# coding: utf-8

from PyQt5.QtWidgets import (
    QHBoxLayout,
    QLabel,
    QVBoxLayout,
    QApplication,
    QDesktopWidget,
)
from PyQt5.QtCore import Qt, pyqtSignal
from PyQt5.QtGui import QPixmap, QFont

from qfluentwidgets import CardWidget, InfoBadge, PushButton, FluentIcon
from app.components.progress_dialog import ProgressDialog
from app.common.install_version import MinecraftVersionInstall

DONWLOAD_SOURCE = 0
MINECRAFT_DIR = "C:\\.mineraft"


if DONWLOAD_SOURCE == 0:
    assetApi = "https://resources.download.minecraft.net"
elif DONWLOAD_SOURCE == 1:
    assetApi = "https://bmclapi2.bangbang93.com/assets"


class VersionCard(CardWidget):
    def __init__(self, parent, version, versionType, jsonUrl):
        super().__init__(parent)
        self.powerNum = 0
        self.version = version
        self.versionType = versionType
        self.jsonUrl = jsonUrl

        clickedBack = pyqtSignal(int)

        self.javaIconLabel = QLabel(self)
        self.versionBadge = InfoBadge(self)

        self.__initWidget()

    def __initWidget(self):
        self.setFixedHeight(80)

        self.javaIconLabel.setFixedSize(50, 50)
        self.javaIconLabel.setPixmap(QPixmap(":app/images/java.png"))
        self.javaIconLabel.setScaledContents(True)

        self.versionBadge.setText(self.version)
        self.versionBadge.setAlignment(Qt.AlignmentFlag.AlignCenter)
        font = QFont()
        font.setFamily("HarmonyOS Sans SC")
        font.setPointSize(10)
        self.versionBadge.setFont(font)
        self.versionBadge.setFixedSize(70, 20)

        self.clicked.connect(self._versionCardClick)

        self.__initLayout()

    def __initLayout(self):
        self.layouts = QHBoxLayout(self)
        self.layouts.setAlignment(
            Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter
        )
        self.layouts.setSpacing(15)
        self.layouts.setContentsMargins(20, 2, 20, 2)

        self.layouts.addWidget(
            self.javaIconLabel,
            0,
            Qt.AlignmentFlag.AlignLeft,
        )

        self.layouts.addWidget(
            self.versionBadge,
            0,
            Qt.AlignmentFlag.AlignLeft,
        )

    def _versionCardClick(self):
        self.powerNum += 1
        print(f'版本" {self.version} "被点击了')

        self.dialog = ProgressDialog("下载任务")
        self.dialog.setWindowModality(Qt.ApplicationModal)
        self.dialog.show()
        center_point = QDesktopWidget().availableGeometry().center()  # 获取屏幕中心点
        self.dialog.move(
            center_point - self.dialog.rect().center()
        )  # 将弹窗移动到屏幕中央

        d = MinecraftVersionInstall()
        back = d.install(
            self.version,
            self.jsonUrl,
            self.progress_callback,
            self.complete_callback,
            self.progress_callback1,
        )
        print(back)

    def progress_callback1(self, a):
        pass

    def complete_callback(self, a):
        print("完成下载========================================")

    def progress_callback(self, a, i):
        self.dialog.updateProgress(int(float(i)))
        print(f"下载进度: {i}%")
